<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="部门">
        <el-select v-model="queryParams.deptid" placeholder="请选择" clearable>
          <el-option
            v-for="item in deptOptions"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:rewards:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:rewards:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rewardsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="部门" align="center" prop="deptName" width="100"/>
      <el-table-column label="员工" align="center" prop="userName" width="100"/>
      <el-table-column label="奖惩类型" align="center" prop="cate" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.cate == '1' ? '惩罚' : '奖励' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.status == '1' ? '未结算' : '结算' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="奖惩金额" align="center" prop="salary" width="100"/>
      <el-table-column label="原因" align="center" prop="reason" width="100"/>
      <el-table-column label="处理意见" align="center" prop="resultx" width="100"/>
      <el-table-column label="备注" align="center" prop="memo" width="100"/>
      <el-table-column label="处理人" align="center" prop="manager" width="100"/>
      <el-table-column label="日期" align="center" prop="addtime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:rewards:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:rewards:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="部门" prop="deptid">
              <el-select v-model="form.deptid" placeholder="请选择" @change="changeUser($event)">
                <el-option
                  v-for="item in deptOptions"
                  :key="item.deptId"
                  :label="item.deptName"
                  :value="item.deptId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工" prop="usersid">
              <el-select v-model="form.usersid" placeholder="请选择">
                <el-option
                  v-for="item in userOptions"
                  :key="item.usersid"
                  :label="item.realname"
                  :value="item.usersid">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类型" prop="cate">
              <el-radio-group v-model="cate">
                <el-radio :label="1">惩罚</el-radio>
                <el-radio :label="2">奖励</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="status">
                <el-radio :label="1">未结算</el-radio>
                <el-radio :label="2">结算</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="罚款金额">
              <el-input-number v-model="form.salary" :min="1" :max="999999" label="金额"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处理日期" prop="addtime">
              <el-date-picker
                v-model="form.addtime"
                type="date"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="原因" prop="reason">
              <el-input v-model="form.reason" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="处理意见" prop="resultx">
              <el-input v-model="form.resultx" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.memo" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listDeptAll} from "@/api/system/dept";
import {
  listRewards,
  listRewardsAll,
  getRewards,
  delRewards,
  addRewards,
  updateRewards,
  listUsers
} from "@/api/rewards/rewards";
import Treeselect from "@riophae/vue-treeselect";

export default {
  name: "Rewards",
  components: {Treeselect},
  data() {
    return {
      ra: 1,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      rewardsList: [],
      deptOptions: [],
      userOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptid: null,
        usersid: null,
        cate: null,
        reason: null,
        resultx: null,
        salary: null,
        addtime: null,
        manager: null,
        status: null,
        memo: null
      },
      cate: 1,
      status: 1,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deptid:[
          { required: true, message: "请选择部门", trigger: "blur" },
        ],
        usersid:[
          { required: true, message: "请选择员工", trigger: "blur" },
        ],
        addtime:[
          { required: true, message: "请选择日期", trigger: "blur" },
        ],
        reason:[
          { required: true, message: "处理原因不能为空", trigger: "blur" },
        ],
        resultx:[
          { required: true, message: "处理意见不能为空", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listRewardsAll(this.queryParams).then(response => {
        this.rewardsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      listDeptAll().then(response => {
        console.log(response.data)
        this.deptOptions = response.data
      });
    },
    changeUser(obj) {
      this.queryParams.deptid = obj
      listUsers(this.addDateRange(this.queryParams)).then(response => {
          this.userOptions = response.rows;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rewardsid: null,
        deptid: null,
        usersid: null,
        cate: null,
        reason: null,
        resultx: null,
        salary: null,
        addtime: null,
        manager: null,
        status: null,
        memo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rewardsid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【奖惩记录】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.userOptions = []
      let obj = {"realname":row.userName,"usersid":row.usersid}
      this.userOptions.push(obj)
      this.reset();
      const rewardsid = row.rewardsid || this.ids
      getRewards(rewardsid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【奖惩记录】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.cate = this.cate
          this.form.status = this.status
          if (this.form.rewardsid != null) {
            updateRewards(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRewards(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
          this.queryParams.deptid = ""
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rewardsids = row.rewardsid || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + rewardsids + '"的数据项？').then(function () {
        return delRewards(rewardsids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/rewards/export', {
        ...this.queryParams
      }, `rewards_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.el-radio__original {
  display: none !important; /* 隐藏原生 radio 输入，但仍然允许交互 */
}
</style>
